// Ofri Keidar 302933981 group 05

/**
 * The class represents a garden apartment. 
 * The class inherits from class Apartment. The class is serializable so a building could be saved and restored from a file easily
 */

package model.apartment;

import java.util.HashMap;
import java.util.Map;

public class GardenApartment extends Apartment {

	private static final long serialVersionUID = 2565801723759517831L; // set serial number
	
	/**
	 * A garden apartment requires the basic parameters a boolean value to indicate whether is has a separate entrance or not.
	 * Therefore, this class extends the basic extractor- uses base class's extracting and extracts the boolean value 
	 */
	public static class Extractor extends BasicExtractor {
		
		/**
		 * Parses parameters using base class's parsing and checks if a boolean value
		 * is given
		 */
		public Map<String, String> parseParameters(String[] parameters) {
			
			// extract basic parameters
			Map<String, String> parameterMap = super.parseParameters(parameters);
			// if basic extraction went wrong, stop parsing
			if (parameterMap.isEmpty()) {
				return parameterMap;
			}
			
			// check if separate entrance indicator is given when no name was entered
			if ("true".equals(parameters[RES_NAME_INDX]) || "false".equals(parameters[RES_NAME_INDX])) {
				// extract separate entrance
				parameterMap.put(SEPARATE_ENTRANCE, parameters[RES_NAME_INDX]);
				
				// set resident name as an empty string
				parameterMap.put(RESIDENT_NAME, "");
				
				// return result
				return parameterMap;
			}
			
			// resident name was given, check next entry			
			if (parameters.length < SEP_ENTRANCE_INDX+1) {
				// not enough parameters were given
				return new HashMap<String, String>();
			}
						
			// check if boolean value for separate entrance is given
			if (!"true".equals(parameters[SEP_ENTRANCE_INDX]) && !"false".equals(parameters[SEP_ENTRANCE_INDX])) {
				// next entry is not a boolean value- invalid format
				return new HashMap<String, String>();
			}
						
			// add boolean value to parameters map
			parameterMap.put(SEPARATE_ENTRANCE, parameters[SEP_ENTRANCE_INDX]);
			// return result
			return parameterMap;
		}
	}
	
	//members
	private static final short FLOOR = 1; // a garden apartment is always on the first floor
	private boolean hasSepEntrance; // TRUE if the apartment has a separated entrance, FALSE if not
	
	// methods

	/**
	 * Constructor- initializes a new garden apartment according to given values
	 * @param roomNum- garden apartment's room number
	 * @param size- garden apartment's size
	 * @param gardenSize- garden apartment's garden size
	 * @param resName- garden apartment's resident name
	 * @param sepEntrance- indicator for separate entrance
	 */
	public GardenApartment(double roomNum, double size, double gardenSize, String resName, boolean sepEntrance) {
		// initialize attributes according to given values
		super(FLOOR, roomNum, size, gardenSize, resName); // use base class's constructor
		this.hasSepEntrance = sepEntrance;
	}
		
	/**
	 * Constructor- initializes a new garden apartment according to given values. 
	 * Used when no resident name is given
	 * @param roomNum- garden apartment's room number
	 * @param size- garden apartment's size
	 * @param gardenSize- garden apartment's garden size
	 * @param sepEntrance- indicator for separate entrance
	 */
	public GardenApartment(double roomNum, double size, double gardenSize, boolean sepEntrance) {
		// initialize attributes according to given values
		super(FLOOR, roomNum, size, gardenSize); // use base class's constructor
		this.hasSepEntrance = sepEntrance;
	}
	
	/**
	 * Returns indicator for separate entrance
	 * @return True if there is a separate entrance, False if not
	 */
	public boolean hasSepEntrance() { return hasSepEntrance; }

	/**
	 * Returns garden area, using base class's extra unit getter 
	 * @return- garden's area attribute
	 */
	public double getGardenArea() { return getExtraUnit(); }
	
	/**
	 * Sets garden area as given value
	 * @param newArea- new area for garden
	 */
	public void setGardenArea(double newArea) { setExtraUnit(newArea); }
	
	/**
	 * Calculates garden apartment's total area as sum of garden apartment's size and garden 
	 */
	public double totalArea() {
		// sum size and garden area and return result
		return getSize() + getExtraUnit(); 
	}
	
	/**
	 * Returns garden apartment's printing format (as a String object) using base class's toString()
	 */
	public String toString() {		
		// use Apartment class's string format to create output
		return "Type: garden apartment, " + super.toString();		
	}
}